package com.hxx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxx.entity.TrainEntity;
import com.hxx.service.TrainSeatService;
import com.hxx.service.TrainService;
import com.hxx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/train")
public class TrainController {
    @Autowired
    private TrainService trainService;

    @Autowired
    private TrainSeatService trainSeatService;

    @GetMapping("page")
    public R listPage(Page page, TrainEntity station) {
        trainService.listPage(page, station);
        return R.ok().data("list",page.getRecords())
                .data("total",page.getTotal());
    }

    @PostMapping("add")
    public R add(@RequestBody TrainEntity station) {
        trainService.save(station);
        return R.ok();
    }

    @PutMapping("update")
    public R update(@RequestBody TrainEntity station) {
        trainService.updateById(station);
        return R.ok();
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Long id) {
        trainService.removeById(id);
        return R.ok();
    }

    @GetMapping("allTrain")
    public R list() {
        return R.ok().data("list",trainService.list());
    }

    @GetMapping("gen/{trainCode}")
    public R generateSeat(@PathVariable String trainCode) {
        trainSeatService.generateSeat(trainCode);
        return R.ok();
    }
}























